package com.jeesite.modules.exam.quota.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.annotation.RepeatSubmit;
import com.jeesite.modules.exam.assessment.entity.DcSupExamInfo;
import com.jeesite.modules.exam.assessment.service.DcSupExamInfoService;
import com.jeesite.modules.exam.quota.entity.DcSupExamQuota;
import com.jeesite.modules.exam.quota.entity.DcSupExamQuotaInfo;
import com.jeesite.modules.exam.quota.entity.DcSupExamSumQuotaInfo;
import com.jeesite.modules.exam.quota.service.DcSupExamQuotaInfoService;
import com.jeesite.modules.exam.quota.service.DcSupExamQuotaService;
import com.jeesite.modules.exam.quota.service.DcSupExamTargetAuditService;
import com.jeesite.modules.exam.score.entity.DcSupExamSumScore;
import com.jeesite.modules.home.entity.cuibanDto;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.action.dao.DcSupTaskActionDao;
import com.jeesite.modules.task.deploy.entity.OfficeUser;
import com.jeesite.modules.utils.OfficeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 指标审核Controller
 * @author cjs
 * @version 2022-05-31
 */
@Controller
@RequestMapping(value = "${adminPath}/targetAudit")
public class DcSupExamTargetAuditController extends BaseController {
    @Autowired
    private DcSupExamTargetAuditService targetAuditService;

    @Autowired
    private DcSupExamInfoService dcSupExamInfoService;

    @Autowired
    private DcSupExamQuotaService dcSupExamQuotaService;
    @Autowired
    private DcSupExamTargetAuditService dcSupExamTargetAuditService;
    @Autowired
    private DcSupTaskActionDao dcSupTaskActionDao;
    /**
     * 查询指标审核列表页面
     */
    //@RequiresPermissions("exam:dcSupExamInfo:view")
    @RequestMapping(value = "auditList")
    public String list(DcSupExamQuotaInfo dcSupExamInfo, Model model) {
        model.addAttribute("dcSupExamInfo", dcSupExamInfo);
        Integer currentYear = Integer.parseInt(DateUtils.getYear());
        model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
        model.addAttribute("currentYear", currentYear);
        return "modules/targetaudit/dcSupExamAuditInfoList";
    }

    /**
     * 查询指标审核列表数据
     */
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<DcSupExamInfo> listData(DcSupExamInfo dcSupExamInfo, HttpServletRequest request, HttpServletResponse response) {
        dcSupExamInfo.setPage(new Page<>(request, response));
        Page<DcSupExamInfo> page = targetAuditService.findPage(dcSupExamInfo);
        return page;
    }

    /**
     * 单项指标考核细则审核页面
     */
    @RequestMapping(value ="targetAuditPage")
    public String list(DcSupExamQuota dcSupExamQuota, Model model, String type) {
        model.addAttribute("dcSupExamQuota", dcSupExamQuota);
        return "modules/targetaudit/dcSupExamScoreXZAudit";
    }

    /**
     * 单项指标考核细则审核列表页面
     */
    @RequestMapping("subView")
    public String subView(DcSupExamQuota dcSupExamQuota, Model model){
        DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo=dcSupExamQuotaService.getByExamIdAndCreateorg(dcSupExamQuota);
        if(dcSupExamSumQuotaInfo==null){
            model.addAttribute("dcSupExamSumQuotaInfo", new DcSupExamSumQuotaInfo());
            model.addAttribute("dcSupExamQuota", new DcSupExamQuota());
            return "modules/targetaudit/dcSupExamScoreXZSubAudit";
        }
        if("1".equals(dcSupExamSumQuotaInfo.getFileStatus())){
            DcSupExamSumQuotaInfo df=new DcSupExamSumQuotaInfo();
            df.setFileStatus("1");
            df.setGradeOrg(dcSupExamSumQuotaInfo.getGradeOrg());
            model.addAttribute("dcSupExamSumQuotaInfo",df);
            model.addAttribute("dcSupExamQuota", dcSupExamQuota);
            return "modules/targetaudit/dcSupExamScoreXZSubAudit";
        }
        if("3".equals(dcSupExamSumQuotaInfo.getFileStatus())){
            DcSupExamSumQuotaInfo df=new DcSupExamSumQuotaInfo();
            df.setFileStatus("3");
            model.addAttribute("dcSupExamSumQuotaInfo",df);
            model.addAttribute("dcSupExamQuota", dcSupExamQuota);
            return "modules/targetaudit/dcSupExamScoreXZSubAudit";
        }
        model.addAttribute("dcSupExamSumQuotaInfo", dcSupExamSumQuotaInfo);
        model.addAttribute("dcSupExamQuota",dcSupExamQuota);
        return "modules/targetaudit/dcSupExamScoreXZSubAudit";
    }

    /**
     * 关键指标报送通知审核页面
     */
    @RequestMapping(value ="targetSendAuditPage")
    public String gjzblist(DcSupExamQuota dcSupExamQuota, Model model,String quotaGroup) {
        String examId = dcSupExamQuota.getExamId();
        DcSupExamInfo dcSupExamInfo = dcSupExamInfoService.get(examId);
        if (dcSupExamInfo!=null){
            dcSupExamQuota.setExamTitle(dcSupExamInfo.getExamTitle());
            dcSupExamQuota.setExamYear(dcSupExamInfo.getExamYear());
        }
        model.addAttribute("dcSupExamQuota", dcSupExamQuota);
        model.addAttribute("quotaGroup", quotaGroup);
        return "modules/targetaudit/dcSupExamDXOrGJZBAuditList";
    }

    /**
     * 单项指标报送通知审核页面
     */
    @RequestMapping(value ="targetSendDXAuditPage")
    public String dxzblist(DcSupExamQuota dcSupExamQuota, Model model,String quotaGroup) {
        String examId = dcSupExamQuota.getExamId();
        DcSupExamInfo dcSupExamInfo = dcSupExamInfoService.get(examId);
        if (dcSupExamInfo!=null){
            dcSupExamQuota.setExamTitle(dcSupExamInfo.getExamTitle());
            dcSupExamQuota.setExamYear(dcSupExamInfo.getExamYear());
        }
        model.addAttribute("dcSupExamQuota", dcSupExamQuota);
        model.addAttribute("quotaGroup", quotaGroup);
        return "modules/targetaudit/dcSupExamDXOrGJZBAuditList";
    }

    /**
     * 关键指标考核审核列表页面
     */
    @RequestMapping("gjzbSubView")
    public String dxzbSubView(DcSupExamQuota dcSupExamQuota, Model model,String quotaGroup){
        String auditStatus ="1";
        String submitStatus =null;
        String examId = dcSupExamQuota.getExamId();
        DcSupExamInfo dcSupExamInfo = dcSupExamInfoService.get(examId);
        //未查询到数据  直接返回空的列表
        if(dcSupExamInfo==null){
            model.addAttribute("dcSupExamQuotaInfoList",new ArrayList<>());
            model.addAttribute("dcSupExamQuota", dcSupExamQuota);
            model.addAttribute("dcSupExamQuotaInfo", new DcSupExamQuotaInfo());
            model.addAttribute("auditStatus",auditStatus);
            model.addAttribute("quotaGroup", quotaGroup);
            return "modules/targetaudit/dcSupExamGJZBSubAuditList";
        }
        int dataTotal = targetAuditService.dcSupExamScoreDataTotal2(dcSupExamQuota);
        if (dataTotal > 0 ) {
            auditStatus = "2";
            dcSupExamQuota.setSubmitStatus("4");
        }
        if (dcSupExamInfo!=null){
            dcSupExamQuota.setExamTitle(dcSupExamInfo.getExamTitle());
            dcSupExamQuota.setExamYear(dcSupExamInfo.getExamYear());
        }
        List<DcSupExamQuotaInfo> list = new ArrayList<>();
        list.add(new DcSupExamQuotaInfo());
        DcSupExamQuota dcSupExam = targetAuditService.getDcSupExamQuotaById(dcSupExamQuota);
        if(dcSupExam==null){
            dcSupExamQuota.setSubmitStatus("2");
            dcSupExam = targetAuditService.getDcSupExamQuotaById(dcSupExamQuota);
        }
        if(dcSupExam==null){
            dcSupExamQuota.setSubmitStatus("0");
            model.addAttribute("dcSupExamQuotaInfoList",new ArrayList<>());
            model.addAttribute("dcSupExamQuota", dcSupExamQuota);
            model.addAttribute("dcSupExamQuotaInfo", new DcSupExamQuotaInfo());
            model.addAttribute("auditStatus","0");
            model.addAttribute("quotaGroup", quotaGroup);
            return "modules/targetaudit/dcSupExamGJZBSubAuditList";
        }
        dcSupExamQuota.setId(dcSupExam.getId());
        //将指标列表数据放入页面
        if(StringUtils.equals(dcSupExamQuota.getSubmitStatus(),"1")){
            submitStatus=dcSupExamQuota.getSubmitStatus();
            dcSupExamQuota.setSubmitStatus("2");
        }
        model.addAttribute("dcSupExamQuotaInfoList",targetAuditService.dcSupExamZBData(dcSupExamQuota));
        if (null != dcSupExam) {
            dcSupExamQuota.setContactName(dcSupExam.getContactName());
            dcSupExamQuota.setContactPhone(dcSupExam.getContactPhone());
            dcSupExamQuota.setId(dcSupExam.getId());
            dcSupExamQuota.setNoticeId(dcSupExam.getNoticeId());
            dcSupExamQuota.setFileStatus(dcSupExam.getFileStatus());
        }
        if(submitStatus!=null){
            dcSupExamQuota.setSubmitStatus(submitStatus);
        }
        if("3".equals(dcSupExam.getSubmitStatus())){
            model.addAttribute("dcSupExamQuotaInfoList",new ArrayList<>());
            model.addAttribute("dcSupExamQuota", new DcSupExamQuota());
        }else {
            model.addAttribute("dcSupExamQuota", dcSupExamQuota);
        }
        model.addAttribute("dcSupExamQuotaInfo", new DcSupExamQuotaInfo());
        model.addAttribute("auditStatus",auditStatus);
        model.addAttribute("quotaGroup", quotaGroup);
        return "modules/targetaudit/dcSupExamGJZBSubAuditList";
    }

    /**
     * 单项指标复制单项指标报送
     */
    @RequestMapping("dxSubCopy")
    @ResponseBody
    public String dxSubCopy(DcSupExamQuota dcSupExamQuota, Model model){
        try {
            DcSupExamQuota deq=dcSupExamTargetAuditService.dxSubCopy(dcSupExamQuota,model);
            if(deq==null){
                return renderResult(Global.TRUE,"","");
            }else {
                return renderResult(Global.TRUE,"",deq.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return renderResult(Global.FALSE,"系统异常！");
        }
    }

    /**
     * 单项指标考核审核列表页面
     */
    @RequestMapping("dxzbSubView")
    public String dxSubView(DcSupExamQuota dcSupExamQuota, Model model){
        DcSupExamQuota dseq=dcSupExamQuotaService.get(dcSupExamQuota);
        if(dseq==null){
            model.addAttribute("dcSupExamQuota",dcSupExamQuota);
        }else {
            dseq.setExamTitle(dcSupExamQuota.getExamTitle());
            model.addAttribute("dcSupExamQuota",dseq);
        }
        return "modules/targetaudit/dcSupExamDXZBSubAuditList";
    }

    /**
     * 单项指标考核细则审核页面查询被考核单位
     */
    @ResponseBody
    @RequestMapping(value = "exameOrgListData")
    public List<Map<String,String>> exameOrgListData(String createOrgName,String examYear,String examId) {
        return targetAuditService.exameOrgListData(createOrgName,examYear,examId);
    }

    /**
     * 单项/关键指标考核指标报送单位
     */
    @ResponseBody
    @RequestMapping(value = "exameSendOrgListData")
    public List<Map<String,String>> exameSendOrgListData(String createOrgName,String type, String examYear,String examId) {
        return targetAuditService.exameSendOrgListData(createOrgName,type,examYear,examId);
    }

    //关键指标审核退回功能
    @ResponseBody
    @RequestMapping(value = "exameSendBack")
    public String exameSendBack(DcSupExamQuota dcSupExamQuota) {
        DcSupExamInfo dcSupExamInfo = dcSupExamInfoService.get(dcSupExamQuota.getExamId());
        if (dcSupExamInfo==null){
            return renderResult(Global.FALSE,"当前考核任务不存在！");
        }
        int dataTotal = targetAuditService.dcSupExamScoreDataTotal2(dcSupExamQuota);
        if (dataTotal > 0 ) {
            return renderResult(Global.FALSE, text("退回失败,指标已审核！"));
        }
        targetAuditService.exameSendBack(dcSupExamQuota);
        return renderResult(Global.TRUE, text("退回成功！"));
    }
    //单项指标审核退回功能
    @ResponseBody
    @RequestMapping(value = "exameSendDXBack")
    public String exameSendDXBack(DcSupExamQuota dcSupExamQuota) {
        try {
            String dcSupExamQuotaId=dcSupExamQuota.getId();
            //根据通知查询原上报的指标
            dcSupExamQuota=dcSupExamQuotaService.getDcSupExamQuota(dcSupExamQuota);
            if (dcSupExamQuota==null){
                return renderResult(Global.FALSE,"退回失败,指标已审核！");
            }
            targetAuditService.exameSendDXBack(dcSupExamQuotaId,dcSupExamQuota.getId(),dcSupExamQuota);
            return renderResult(Global.TRUE, text("退回成功！"));
        } catch (Exception e) {
            e.printStackTrace();
            return renderResult(Global.FALSE, text("退回失败，系统异常"));
        }
    }

    //审核退回功能
    @ResponseBody
    @RequestMapping(value = "exameZBSendBack")
    public String exameZBSendBack(@RequestParam(value = "exameOrgs" ,required=false) String exameOrgs) {
        targetAuditService.exameZBSendBack(exameOrgs);
        return renderResult(Global.TRUE, text("退回成功！"));
    }

    /**
     * 关键指标审核
     */
    @PostMapping(value = "saveAuditIndex")
    @ResponseBody
    public String saveAuditIndex(@Validated DcSupExamQuota dcSupExamQuota,String dcSupExamSumQuotaDtoListStr,String typeIndex) {
        try {
            int dataTotal = targetAuditService.dcSupExamScoreDataTotal2(dcSupExamQuota);
            if (dataTotal > 0 ) {
                return renderResult(Global.FALSE, text("审核失败,指标已审核！"));
            }
            targetAuditService.saveAuditIndex(dcSupExamQuota,dcSupExamSumQuotaDtoListStr,typeIndex);
        } catch (Exception e) {
            e.printStackTrace();
            return renderResult(Global.FALSE, text("审核失败！"));
        }
        return renderResult(Global.TRUE, text("审核成功！"));
    }

    /**
     * 单项关键指标审核
     */
    @PostMapping(value = "dxsaveAuditIndex")
    @ResponseBody
    public String dxsaveAuditIndex(@Validated DcSupExamQuota dcSupExamQuota) {
        try {
            String dcSupExamQuotaId=dcSupExamQuota.getId();
            dcSupExamQuota=dcSupExamQuotaService.getDcSupExamQuota(dcSupExamQuota);
            if (dcSupExamQuota==null) {
                return renderResult(Global.FALSE, text("审核失败,指标已审核！"));
            }
            targetAuditService.dxsaveAuditIndex(dcSupExamQuotaId,dcSupExamQuota);
            return renderResult(Global.TRUE, text("审核成功！"));
        } catch (Exception e) {
            e.printStackTrace();
            return renderResult(Global.FALSE, text("审核失败！"));
        }
    }

    /**
     * 单项细则审核
     */
    @PostMapping(value = "littleAuditIndex")
    @ResponseBody
    public String littleAuditIndex(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
        try {
            int dataTotal = targetAuditService.dcSupExamScoreDataTotal(dcSupExamSumQuotaInfo);
            if (dataTotal > 0 ) {
                return renderResult(Global.FALSE, text("审核失败,细则已审核！"));
            }
            String message=dcSupExamTargetAuditService.littleAuditIndex(dcSupExamSumQuotaInfo);
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            return renderResult(Global.FALSE, text("审核失败，系统异常"));
        }
    }

    /**
     * 细则退回
     * @param dcSupExamSumQuotaInfo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "litteSendBack")
    public String litteSendBack(DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo) {
        try {
            int dataTotal = targetAuditService.dcSupExamScoreDataTotal(dcSupExamSumQuotaInfo);
            if (dataTotal > 0 ) {
                return renderResult(Global.FALSE, text("退回失败,指标已审核！"));
            }
            String message=dcSupExamTargetAuditService.litteSendBack(dcSupExamSumQuotaInfo);
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            return renderResult(Global.FALSE, text("退回失败，系统异常"));
        }
    }

    /**
     * doubaolin备注:已弃用,修改催办需要选人
     * @param dcSupExamQuota
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "urgeInformation")
    public String urgeInformation(DcSupExamQuota dcSupExamQuota){
        Boolean b=targetAuditService.UrgeInformation(dcSupExamQuota);
        if(b){
            return renderResult(Global.TRUE, text("催办成功！"));
        }else {
            return renderResult(Global.FALSE,text("催办失败！"));
        }
    }

    /**
     * 关键指标审核催办
     * @param cuiban
     * @return
     */
    @RequestMapping(value = "cuibanGJ")
    @ResponseBody
    @RepeatSubmit
    public String cuibanGJ(cuibanDto cuiban) {
        targetAuditService.cuibanGJ(cuiban);
        return renderResult(Global.TRUE, text("催办成功!"));
    }
    /**
     * 单项指标审核催办
     * @param cuiban
     * @return
     */
    @RequestMapping(value = "cuibanDX")
    @ResponseBody
    @RepeatSubmit
    public String cuibanDX(cuibanDto cuiban) {
        targetAuditService.cuibanDX(cuiban);
        return renderResult(Global.TRUE, text("催办成功!"));
    }

    @RequestMapping(value = "cuibanXZ")
    @ResponseBody
    @RepeatSubmit
    public String cuibanXZ(cuibanDto cuiban) {
        targetAuditService.cuibanXZ(cuiban);
        return renderResult(Global.TRUE, text("催办成功!"));
    }


    @RequestMapping(value = {"cuibanPageGJ", ""})
    public String cuibanPageGJ(cuibanDto cuiban , Model model) {
        // 定义催办的初始文本
        cuiban.setMessage("您有一个关键指标审核被催办,请及时查看");
        // 定义临时变量
        String tempUserCode = "";
        String tempUserName = "";
        List<Map<String, String>> listUser = new ArrayList<>();

        // 获取考核单位角色
        List<OfficeUser> userByOrg = dcSupTaskActionDao.getUserByOrg("EL_ORG", cuiban.getOrg());
        if (userByOrg != null && userByOrg.size() > 0) {
            // 遍历集合，设置页面初始值
            for (int i = 0; i < userByOrg.size(); i++) {
                User transferUser = UserUtils.get(userByOrg.get(i).getUserCode());
                Map<String, String> map = new HashMap<>();
                map.put("name", transferUser.getUserName());
                map.put("userCode", transferUser.getUserCode());
                map.put("phone", transferUser.getMobile());
                map.put("id", transferUser.getUserCode());
                listUser.add(map);
                tempUserCode = tempUserCode + "," + transferUser.getUserCode();
                tempUserName = tempUserName + "," + transferUser.getUserName();
            }
            // 过滤字符串第一个逗号
            tempUserCode = tempUserCode.substring(1);
            tempUserName = tempUserName.substring(1);
        }

        model.addAttribute("defaultUserList", listUser);
        cuiban.setUserCode(tempUserCode);
        cuiban.setName(tempUserName);
        model.addAttribute("cuibanPage", cuiban);
        model.addAttribute("orgCode", cuiban.getOrg());
        return "modules/targetaudit/cuibanPageGJ";
    }
    @RequestMapping(value = {"cuibanPageXZ", ""})
    public String cuibanPageXZ(cuibanDto cuiban , Model model) {
        // 定义催办的初始文本
        cuiban.setMessage("您有一个细则审核被催办,请及时查看");
        // 定义临时变量
        String tempUserCode = "";
        String tempUserName = "";
        List<Map<String, String>> listUser = new ArrayList<>();

        // 获取考核单位角色
        List<OfficeUser> userByOrg = dcSupTaskActionDao.getUserByOrg("EL_ORG", cuiban.getOrg());
        if (userByOrg != null && userByOrg.size() > 0) {
            // 遍历集合，设置页面初始值
            for (int i = 0; i < userByOrg.size(); i++) {
                User transferUser = UserUtils.get(userByOrg.get(i).getUserCode());
                Map<String, String> map = new HashMap<>();
                map.put("name", transferUser.getUserName());
                map.put("userCode", transferUser.getUserCode());
                map.put("phone", transferUser.getMobile());
                map.put("id", transferUser.getUserCode());
                listUser.add(map);
                tempUserCode = tempUserCode + "," + transferUser.getUserCode();
                tempUserName = tempUserName + "," + transferUser.getUserName();
            }
            // 过滤字符串第一个逗号
            tempUserCode = tempUserCode.substring(1);
            tempUserName = tempUserName.substring(1);
        }

        model.addAttribute("defaultUserList", listUser);
        cuiban.setUserCode(tempUserCode);
        cuiban.setName(tempUserName);
        model.addAttribute("cuibanPage", cuiban);
        model.addAttribute("orgCode", cuiban.getOrg());
        return "modules/targetaudit/cuibanPageXZ";
    }
    @RequestMapping(value = {"cuibanPageDX", ""})
    public String cuibanPageDX(cuibanDto cuiban , Model model) {
        // 定义催办的初始文本
        cuiban.setMessage("您有一个单项指标审核被催办,请及时查看");
        // 定义临时变量
        String tempUserCode = "";
        String tempUserName = "";
        List<Map<String, String>> listUser = new ArrayList<>();

        // 获取考核单位角色
        List<OfficeUser> userByOrg = dcSupTaskActionDao.getUserByOrg("EL_ORG", cuiban.getOrg());
        if (userByOrg != null && userByOrg.size() > 0) {
            // 遍历集合，设置页面初始值
            for (int i = 0; i < userByOrg.size(); i++) {
                User transferUser = UserUtils.get(userByOrg.get(i).getUserCode());
                Map<String, String> map = new HashMap<>();
                map.put("name", transferUser.getUserName());
                map.put("userCode", transferUser.getUserCode());
                map.put("phone", transferUser.getMobile());
                map.put("id", transferUser.getUserCode());
                listUser.add(map);
                tempUserCode = tempUserCode + "," + transferUser.getUserCode();
                tempUserName = tempUserName + "," + transferUser.getUserName();
            }
            // 过滤字符串第一个逗号
            tempUserCode = tempUserCode.substring(1);
            tempUserName = tempUserName.substring(1);
        }

        model.addAttribute("defaultUserList", listUser);
        cuiban.setUserCode(tempUserCode);
        cuiban.setName(tempUserName);
        model.addAttribute("cuibanPage", cuiban);
        model.addAttribute("orgCode", cuiban.getOrg());
        return "modules/targetaudit/cuibanPageDX";
    }
}
